/*
 * Copyright (c) 2006-2022, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author        Notes
 * 2022-10-10     Liuxiequan    first version
 */
#include <rtthread.h>
#include "hal_data.h"
#include <rtdevice.h>

/*
 *  See drv_pwm_controL.c, only support channel 0
 *
 * There's actually only one GPT timer with 10 channels. In this case, the
 * timer is separated into 10 PWM devices, so each device has only one
 * channel.
 *
 */
#define PWM_DEV_CHANNEL    0

rt_uint32_t period, pulse, dir;

int pwm_test(int argc, char**argv)
{
    struct rt_device_pwm *pwm_dev;
int pulse;
int period;

    if ((argc != 4) || (1 != sscanf(argv[2], "%lu", &period)) || (1 != sscanf(argv[3], "%lu", &pulse)))
    {
        rt_kprintf("Please input \"mpwm_test <pwm0~pwm9> <period> <pulse>\"\n");
        return -1;
    }
            

	if(argv[1][3] != '2')
	{
		rt_kprintf("Only support pwm2\n");
		return -2;
	}

	if(pulse > period)
	{
		rt_kprintf("PWM pulse need min than period\n");
		return -2;
	}

    pwm_dev = (struct rt_device_pwm *)rt_device_find(argv[1]);
    if (pwm_dev == RT_NULL)
    {
        rt_kprintf("pwm sample run failed! can't find %s device!\n", argv[1]);
        return RT_ERROR;
    }

    rt_pwm_set(pwm_dev, PWM_DEV_CHANNEL, period, pulse);
    rt_pwm_enable(pwm_dev, PWM_DEV_CHANNEL);

    return 0;
}

MSH_CMD_EXPORT(pwm_test, Usage: pwm_test <pwm0~pwm9> <period> <pulse>);